#!/usr/bin/env python
# 待爬取的目标网站的一些属性
from urllib.parse import urlparse


class TargetWebsite(object):

    @property
    def url(self):
        if not hasattr(self, "_url"):
            self._url = None
        return self._url

    @url.setter
    def url(self, value):
        if not value:
            raise ValueError("url不能为空!")
        self._url = value

    @property
    def charset(self):
        if not hasattr(self, "_charset") or not self._charset:
            self._charset = "utf-8"
        return self._charset

    @charset.setter
    def charset(self, value):
        if not value:
            raise ValueError("编码类型不能为空!")
        self._charset = value

    @property
    def name(self):
        if not hasattr(self, "_name"):
            self._name = None
        return self._name

    @name.setter
    def name(self, value):
        if not value:
            raise ValueError("网站名不能为空!")
        self._name = value

    # 解析url的域名
    @property
    def domain(self):
        # 解析域名
        parsed_uri = urlparse(self.url)
        return "{uri.scheme}://{uri.netloc}".format(uri=parsed_uri)

    @property
    def listSelectorSyntax(self):
        if not hasattr(self, "_listSelectorSyntax"):
            self._listSelectorSyntax = None
        return self._listSelectorSyntax

    @listSelectorSyntax.setter
    def listSelectorSyntax(self, value):
        if not value:
            raise ValueError("CSS选择器语法不能为空!")
        self._listSelectorSyntax = value

    @property
    def contentSelectorSyntax(self):
        if not hasattr(self, "_contentSelectorSyntax"):
            self._contentSelectorSyntax = None
        return self._contentSelectorSyntax

    @contentSelectorSyntax.setter
    def contentSelectorSyntax(self, value):
        if not value:
            raise ValueError("CSS选择器语法不能为空!")
        self._contentSelectorSyntax = value

    def __init__(self, name, url, charset, listSelectorSyntax, contentSelectorSyntax):
        self.charset = charset
        self.name = name
        self.url = url
        self.listSelectorSyntax = listSelectorSyntax
        self.contentSelectorSyntax = contentSelectorSyntax
